package cn.breeze.basic.listener;

import cn.breeze.basic.service.IPermissionScanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener
@Slf4j
public class PermissionScanInitInstener implements ServletContextListener {

    @Autowired
    private IPermissionScanService permissionScanService;

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {

        // 为了防止我们的代码比较复杂影响项目启动速度，我们创建线程来运行
        new Thread(new Runnable(){
            @Override
            public void run() {
                log.info("contextInitialized start...");
                // 执行permission数据添加方法
                permissionScanService.scanPermission();
                log.info("contextInitialized stop...");
            }
        }).start();
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        log.info("contextDestroyed start.....");
    }
}
